package com.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mapper.CategoryMapper;
import com.model.Category;
import com.util.PageBean;
@Service
public class CategoryServiceImpl implements CategoryService{
        
	@Autowired
	private CategoryMapper categoryMapper;

	//查询多条记录
	public List<Category> queryCategoryList(Category category,PageBean page) throws Exception {
		Map<String, Object> map =getQueryMap(category, page);
		
		List<Category> getCategory = categoryMapper.query(map);
		
		return getCategory;
	}
	
	//得到记录总数
	@Override
	public int getCount(Category category) {
		Map<String, Object> map = getQueryMap(category, null);
		int count = categoryMapper.getCount(map);
		return count;
	}
	
	private Map<String, Object> getQueryMap(Category category,PageBean page){
		Map<String, Object> map = new HashMap<String, Object>();
		if(category!=null){
			map.put("tid", category.getTid());
			map.put("tname", category.getTname());
			map.put("sort", category.getSort());
			map.put("condition", category.getCondition());

		}
		PageBean.setPageMap(map, page);
		return map;
	}
		
	//添加
	public int insertCategory(Category category) throws Exception {
		return categoryMapper.insertCategory(category);
	}

	//根据ID删除
	public int deleteCategory(int id) throws Exception {
		return categoryMapper.deleteCategory(id);
	}

	//更新
	public int updateCategory(Category category) throws Exception {
		return categoryMapper.updateCategory(category);
	}
	
	//根据ID得到对应的记录
	public Category queryCategoryById(int id) throws Exception {
		Category po =  categoryMapper.queryCategoryById(id);
		return po;
	}
}

